package com.zsrt.examination.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

import com.zsrt.examination.dao.FunctionDao;
import com.zsrt.examination.entity.StudentEntity;
import com.zsrt.utils.AscAgeComparator;
import com.zsrt.utils.ListUtil;

/**
*@author jzh
*@创建时间 2017年4月20日下午6:38:51
*类说明：
*/
public class FunctionDaoImpl implements FunctionDao {
	
	List<StudentEntity> studentLists=new ArrayList<StudentEntity>();
	public List<StudentEntity> init(){
		
		StudentEntity zsh=new StudentEntity("user","222","曾少华",22,"222","安徽","合肥","222",112,112,112);
		StudentEntity jmj=new StudentEntity("user","111","贾梦菊",21,"111","安徽","合肥","111",111,111,111);
		StudentEntity zc=new StudentEntity("user","333","周驰", 23,"333","湖北","武汉","333",113,113,113);
		StudentEntity zjy=new StudentEntity("user","444","张嘉源",24,"444","湖北","武汉","444",114,114,114);
		StudentEntity jzh=new StudentEntity("user","555","靳志辉",25,"555","安徽","合肥","555",115,115,115);
		StudentEntity lzh=new StudentEntity("user","666","罗智壕",26,"666","广东","深圳","666",116,116,116);
		StudentEntity hxq=new StudentEntity("user","777","何小强",27,"777","安徽","芜湖","777",117,117,117);
		
		List<StudentEntity> studentList=new ArrayList<StudentEntity>();
		studentList.add(jmj);
		studentList.add(zsh);
		studentList.add(zc);
		studentList.add(zjy);
		studentList.add(jzh);
		studentList.add(lzh);
		studentList.add(hxq);
		return studentList;
	}

	@Override
	public void printLoginMenu() {
		

	}

	@Override
	public void printAdminFirstMenu(){
		System.err.println("\t\t _____________________________");
		System.err.println("\t\t         欢迎进入管理员主界面                       ");
		System.err.println("\t\t —————————————————————————————");
		System.err.println("\t\t          1.录入考生信息                 ");
		System.err.println("\t\t          2.修改考生信息              ");
		System.err.println("\t\t          3.删除考生信息              ");
		System.err.println("\t\t          4.查询考生信息              ");
		System.err.println("\t\t          5. 返回上一层                       ");
		System.err.println("\t\t —————————————————————————————");
		System.err.println("请输入相应的操作数:");

		
	}
	
	
	public void printUserFirstMenu(){
		System.err.println("\t\t _____________________________");
		System.err.println("\t\t               欢迎进入考生主界面        ");
		System.err.println("\t\t —————————————————————————————");
		System.err.println("\t\t          1.我的信息                 ");
		System.err.println("\t\t          2.查询本市总分状元              ");
		System.err.println("\t\t          3.查询全国各个省份的状元              ");
		System.err.println("\t\t          4.查询指定城市的状元              ");
		System.err.println("\t\t          5. 成绩按降序排列                       ");
		System.err.println("\t\t          6. 返回上一层                       ");
		System.err.println("\t\t —————————————————————————————");
		System.err.println("请输入相应的操作数:");
	}

	@Override
	public void printMyDataMenu() {
		System.err.println("\t\t _____________________________");
		System.err.println("\t\t         欢迎进入我的信息主界面        ");
		System.err.println("\t\t —————————————————————————————");
		System.err.println("\t\t          1.查看个人详细信息                 ");
		System.err.println("\t\t          2.修改个人信息              ");
		System.err.println("\t\t          3. 返回上一层                       ");
		System.err.println("\t\t —————————————————————————————");
		System.err.println("请输入相应的操作数:");

		
	}
	
	public void printToUpper(){
		System.err.println("\t\t _____________________________");
		System.err.println("\t\t            1 返回上一级                               ");
		System.err.println("\t\t —————————————————————————————");
		System.err.println("请输入相应的操作数:");
	}
	
	@Override
	public boolean importAdminIdAndPw(Scanner in) {
		System.err.println("请输入手机号码");
		String id = in.next();
		System.err.println("请输入密码");
		String pw = in.next();
		if(id.equals("admin")&&pw.equals("admin")){
			return true;
		}
		return false;
	}
	
	@Override
	public boolean importUserIdAndPw(List<StudentEntity> initList,String id,String pw) {
		
		for(StudentEntity studentList : initList){
			String id1 = studentList.getId();
			String password = studentList.getPassword();
			if(id.equals(id1)&&pw.equals(password)){
				return true;
			}
		}
		return false;
	}
	
	@Override
	public void updateScore(List<StudentEntity> studentLists,Scanner in) {
		StudentEntity student=null;
		
		System.err.println("输入要修改的学生id");
		//获得要修改的id
		String id = in.next();
		//遍历studentLists
		for(int index=0;index<studentLists.size();index++){
			//获得每一个学生对象
			student = studentLists.get(index);
			//得到学生的id
			String id1= student.getId();
			//判断和输入的id是否相等,如果相等，可以继续选择要修改的科目
			if(id.equals(id1)){
				System.err.println("输入要修改的科目  1/语文 2/数学 3/英文");
				int number = in.nextInt();
				//输入1  代表要修改语文成绩
				if(number==1){
					System.err.println("输入修改后的语文成绩");
					int chineseScore = in.nextInt();
					//修改语文成绩
					student.setChineseScore(chineseScore);
					//把新修改后的学生对象放回到原来的studentLists
					studentLists.set(index, student);
					System.err.println(student);
					return;
				}else if(number==2){	
					System.err.println("输入修改后的数学成绩");
					int mathScore = in.nextInt();
					//修改数学成绩
					student.setMathScore(mathScore);
					//把新修改后的学生对象放回到原来的studentLists
					studentLists.set(index, student);
					System.err.println(student);
					return;
				}else{
					System.err.println("输入修改后的英语成绩");
					int englishScore = in.nextInt();
					//修改英语成绩
					student.setEnglishScore(englishScore);
					//把新修改后的学生对象放回到原来的studentLists
					studentLists.set(index, student);
					System.err.println(student);
					return;
				}
			}
		}
		System.err.println("id错误");
	}

	@Override
	public void findAllStudents(List<StudentEntity> studentLists) {
		for(StudentEntity student:studentLists){
			System.err.println(student);
		}
		
	}

	@Override
	public boolean deleteStudentsById(List<StudentEntity> studentLists, String id) {
		boolean flag=false;
		/*for(StudentEntity student:studentLists){
			String id1 = student.getId();
			if(id.equals(id1)){
				System.out.println("......");
				flag = studentLists.remove(student);
				//return flag;
				System.out.println(".....");
			}
		}*/
		for(int index=0;index<studentLists.size();index++){
			StudentEntity student=studentLists.get(index);
			String id1 = student.getId();
			if(id.equals(id1)){
				studentLists.remove(index);
				flag=true;
				return flag;
			}
		}
		return flag;
	}

	@Override
	public void findMyDate(List<StudentEntity> studentList,String id) {
		for(StudentEntity student:studentList){
			String id1=student.getId();
			if(id.equals(id1)){
				System.err.println(student);
				break;
			}
		}
	}

	@Override
	public void descScore(List<StudentEntity> studentLists) {
		/*for(int index=0;index<studentLists.size();index++){
			StudentEntity student = studentLists.get(index);
			
		}*/
		//Collections.sort(studentLists);
		Collections.sort(studentLists, new AscAgeComparator());
		for(StudentEntity student:studentLists){
			System.err.println(student);
		}
		
	}

	@Override
	public void findHighestScoreInCity(List<StudentEntity> studentLists, String city) {
		int highestScore=0;
		int counter=0;
		for(int index=0;index<studentLists.size();index++){
			StudentEntity student = studentLists.get(index);
			String ct=student.getCity();
			if(city.equals(ct)){
				int totalScore=student.getChineseScore()+student.getEnglishScore()+student.getMathScore();
				if(totalScore>highestScore){
					highestScore=totalScore;
					counter=index;
				}
			}
		}
		StudentEntity student = studentLists.get(counter);
		System.out.println(student);
	}
	
	@Override
	public StudentEntity findHighestScoreInProvince(List<StudentEntity> studentLists, String Province) {
		int highestScore=0;
		int counter=0;
		for(int index=0;index<studentLists.size();index++){
			StudentEntity student = studentLists.get(index);
			String pv=student.getProvince();
			if(Province.equals(pv)){
				int totalScore=student.getChineseScore()+student.getEnglishScore()+student.getMathScore();
				if(totalScore>highestScore){
					highestScore=totalScore;
					counter=index;
				}
			}
		}
		StudentEntity student = studentLists.get(counter);
		return student;
	}
	
	@Override
	public void findHighestScoreInProvinces(List<StudentEntity> studentLists) {
		Set<String> set=new HashSet<String>();
		List<StudentEntity> list=new ArrayList<StudentEntity>();
		for(int index=0;index<studentLists.size();index++){
			StudentEntity student = studentLists.get(index);
			String province=student.getProvince();
			set.add(province);
		}
		Iterator<String> iterator = set.iterator();
		while(iterator.hasNext()){
			String province = iterator.next();
			StudentEntity student = findHighestScoreInProvince(studentLists,province);
			list.add(student);
		}
		ListUtil.printList(list);
	}

	

	

	

}

